package springmvc.common;

import Utils.ReflectionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Api(value = "测试controller", tags = {"测试,因为参数我暴露的是实体，需要调用getEntity方法知道实体的名称，注意大小写"})
public class EntityController extends BaseController {


    @RequestMapping(value = "/getEntity", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "获取对象属性", notes = "获取对象属性", httpMethod = "GET")
    public String getEntity() throws ClassNotFoundException, SecurityException, InstantiationException, IllegalAccessException {
        ReflectionUtils ref = new ReflectionUtils();

        List<String> clzList = new ArrayList<String>();
        ref.scanPackages("com.Entity", clzList);


        Map<String, Map<String, Object>> map = new HashMap<String, Map<String, Object>>();
        for (int i = 0; i < clzList.size(); i++) {

            Class clazz = Class.forName(clzList.get(i), false, Thread.currentThread().getContextClassLoader());


            Map<String, Object> item = new HashMap<String, Object>();

            String strClass = clazz.getSimpleName();// 直接获取类名 User


            Field[] declaredFields = clazz.newInstance().getClass().getDeclaredFields();
            for (Field field : declaredFields) {
                field.setAccessible(true);//使可以set私有属性的值


                item.put(field.getName(), field.getGenericType().toString().replace("class ", ""));
            }

            map.put(strClass, item);


        }

        return getResult(map, "获取实体属性", 1);


    }


}
